<template>
	<view class="pagebody" :style="{ height: maxHeight }">
		<view class="topbox">
			<view class=" align-center">
				<view class=" flex-row align-center">
					<image @click="backpage" src="@/static/images/login/leftjt.png" alt=""
						style="width: 48rpx;height: 48rpx;"></image>
				</view>
				<view class="color303b39 fz40 fwbold" style="margin-left: 263rpx;">
					疾病科室
				</view>
			</view>
		</view>
		<view class="flex-row " style="height: 100%;">
			<ul class=""
				style="width: 196rpx;border-radius: 0 16rpx 16rpx 0 ;background: #F6FAFA;padding-bottom: 116rpx;overflow: scroll;">
				<li @click="checksub(item,index)" class="justify-center align-center fz32 fwbold"
					:class="issub==index?'issub':'noissub'" style="width: 196rpx; height: 100rpx;"
					v-for="(item,index) in sublist" :key="index">
					{{item.name}}
				</li>
			</ul>
			<ul class="justify-between ml16 flex-wrap"
				style="width: 516rpx; align-content: start;overflow: scroll; padding-bottom: 120rpx;">
				<li @click="checkdis(item,index)" class="borderbox justify-center align-center mb16 br16 color303b39"
					:class="isdis==item.id?'isdis':'noisdis'" v-for="(item,index) in dislist" :key="index"
					style="font-size: 30rpx;width: 250rpx;height: 104rpx;padding: 0 16rpx;">
					<view class="lineclamp2">
						{{item.name}}
					</view>
				</li>
			</ul>
		</view>



		<view class="botbtn flex-row justify-between">
			<view @click="backpage" class="bgfff fz36 color303b39 justify-center align-center"
				style="height: 100%;border: 1rpx solid #F0F6F5;width: 50%;">
				关闭
			</view>
			<view @click="submit()" class=" fz36 justify-center align-center"
				style="height: 100%;border: 1rpx solid #F0F6F5;width: 50%;background: #00D9B2;color: #fff;">
				确定
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getDiseaseTree,
		editdocument
	} from '@/api/api.js'
	export default {
		components: {},
		onLoad(options) {
			// 获得屏幕最大高度
			let systemInfo = uni.getSystemInfoSync();
			this.maxHeight = systemInfo.windowHeight + "px";
			if (options.setarea == 1) {
				this.issetarea = true
			}
			if (options.setother == 1) {
				this.issetother = true
			}
			if (options.druglist == 1) {
				this.isdruglist = true
			}
		},


		data() {
			return {
				issetarea: false,
				issetother: false,
				isdruglist: false,
				maxHeight: '',
				sublist: [],
				issub: 0,
				dislist: [],
				isdis: 0,
				name: '',
				endid: 0,
				subid: null,
				subname: '',
				disid: null,
				disname: ''
			}
		},
		watch: {},
		mounted() {
			this.getdislist()
		},
		methods: {
			getdislist() {
				getDiseaseTree().then(res => {
					this.sublist = res.data
					this.dislist = this.sublist[0].childrens
					this.subname = this.sublist[0].name
					this.subid = this.sublist[0].id
				})
			},
			checksub(item, index) {
				this.issub = index
				this.name = item.name
				this.dislist = this.sublist[index].childrens
				this.subid = item.id
				this.subname = item.name
				this.disid = null
				this.endid = item.id
			},
			checkdis(item, index) {
				this.isdis = item.id
				this.name = item.name
				this.disid = item.id
				this.disname = item.name
				this.endid = item.id
				this.submit()

			},
			backpage() {
				uni.navigateBack({
					delta: 1,
				});
			},
			submit() {
				if (this.disid == null) {
					this.$refs.uToast.show({
						title: '请选择疾病',
					})
					return;
				} else {
					if (this.issetother) {
						console.log(1)
						let otherobj = {
							departmentId: this.subid,
							departmentName: this.subname,
							diseaseId: this.disid,
							diseaseName: this.disname,
						}
						editdocument(otherobj).then(res => {})

					} else if (this.isdruglist) {
						console.log(2, this.endid);
						console.log('进入 isdruglist 分支');
						uni.redirectTo({
							url: `/pages/knowledge/druglist?id=` + this.endid,
						})
						return; // 确保不会继续执行后续的 uni.navigateBack
					} else {
						console.log(3)
						let obj = {}
						if (this.issetarea) {
							obj = {
								subid: this.subid,
								subname: this.subname,
								disid: this.disid,
								disname: this.disname,
							}
						} else {
							obj = {
								id: this.endid,
								name: this.name
							}
						}
						uni.$emit('returnData', obj)
					}
					uni.navigateBack({
						delta: 1,
					});
				}

			}

		},

	}
</script>

<style lang="scss" scoped>
	.pagebody {
		padding-bottom: 136rpx;
		background: #FFF;

		.topbox {
			height: 128rpx;
			background: linear-gradient(180deg, rgba(0, 217, 178, 0.2) 0%, rgba(0, 217, 178, 0) 100%);
			padding: 20rpx 24rpx;
		}

		.issub {
			background: #FFF;
			color: #303b39;
		}

		.noissub {
			background: #F6FAFA;
			color: #576260;
		}

		.isdis {
			color: #00977C;
			font-weight: bold;
			background: linear-gradient(125deg, #F5FEFC 0%, #E7FCF8 100%);

		}

		.noisdis {
			background: #F6FAFA;
			color: #303b39;
		}

		.botbtn {
			height: 98rpx;
			position: fixed;
			bottom: 0;
			width: 100%;
		}
	}
</style>